﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AD.Contract.Models;
using AD.Contract;
using HZT.Framework.Contract;
using AD.DAL;
using EntityFramework.Extensions;
namespace AD.BLL
{
    public class CategoryService : IBLL.ICategoryService
    {


        public IEnumerable<BaseCategory> GetCategoryList(CategoryRequest request = null )
        {
            request = request ?? new CategoryRequest();
            using (var dbContext = new ADDbContext())
            {
                IQueryable<BaseCategory> categorys = dbContext.BaseCategories;
                
                if (!string.IsNullOrEmpty(request.CategoryName))
                {
                    categorys = categorys.Where(u => u.Name.Contains(request.CategoryName));
                }

                return categorys.OrderByDescending(u => u.Id).ToPagedList(request.PageIndex, request.PageSize);
            }
        }
        public IEnumerable<BaseCategory> GetCategoryList(string CategoryType, CategoryRequest request = null)
        {
            request = request ?? new CategoryRequest();
            using (var dbContext = new ADDbContext())
            {
                IQueryable<BaseCategory> categorys = dbContext.BaseCategories;
                if (!string.IsNullOrEmpty(CategoryType))
                {
                    categorys = categorys.Where(u => u.CategoryType == CategoryType);
                }
                if (!string.IsNullOrEmpty(request.CategoryName))
                {
                    categorys = categorys.Where(u => u.Name.Contains(request.CategoryName));
                }

                return categorys.OrderByDescending(u => u.Id).ToPagedList(request.PageIndex, request.PageSize);
            }
        }

        public bool Add(Chanel chanel)
        {
            using (var dbcontext = new ADDbContext())
            {
                if (chanel.nID > 0)
                {
                    dbcontext.Update<Chanel>(chanel);
                }
                else
                {
                    dbcontext.Insert<Chanel>(chanel);
                }
                return true;

            }
        }

        public void Save(BaseCategory baseCategory)
        {
            using (var dbcontext = new ADDbContext())
            {
                if (!string.IsNullOrEmpty( baseCategory.Id))
                {
                    dbcontext.Update<BaseCategory>(baseCategory);
                }
                else
                {
                    baseCategory.Id = Guid.NewGuid().ToString();

                    dbcontext.Insert<BaseCategory>(baseCategory);
                }
            }
        }
        public BaseCategory GetModel(string id)
        {

            using (var dbcontext = new ADDbContext())
            {
                return dbcontext.Find<BaseCategory>(id);
            }

        }

        public void Delete(List<string> ids)
        {
            using (var dbContext = new ADDbContext())
            {
                dbContext.BaseCategories.Where(u => ids.Contains(u.Id)).Delete();
            }
        }
    }
}